//
//  TTXMLList.m
//  Tint
//
//  Created by Kevin Wu on 4/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTXMLList.h"
#import <objc/runtime.h>


@implementation TTXMLList



#pragma mark - Accessors

@synthesize list = _list;


#pragma mark - Memory

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)dealloc {
    TTRelease(_list);
    [super dealloc];
}


#pragma mark - Override

+ (TTXMLList *)objectWithXMLNode:(TTXMLNode *)XMLNode {
    
    if (XMLNode == nil) return nil;
    
    TTXMLList *object = [[self alloc] init];
    
    NSDictionary *propertyMap = [self propertyMap];
    NSEnumerator *nameEnumerator = [propertyMap keyEnumerator];
    NSString *nodeName = nil;
    while ((nodeName = [nameEnumerator nextObject])) {
        
        NSString *propertyName = [propertyMap objectForKey:nodeName];
        id propertyValue = nil;
        
        
        TTXMLNode *node = [[XMLNode elementsForName:nodeName] firstObject];
        if (node) {
            Class cls = [self classForNodeName:node.name];
            if (cls) propertyValue = [cls objectWithXMLNode:node];
            else propertyValue = node.value;
        }
        
        TTXMLNode *attr = [XMLNode attributeForName:nodeName];
        if (attr) propertyValue = attr.value;
        
        
        if (propertyValue == nil) continue;
        
        
        NSString *attribute = [self attributeForProperty:propertyName];
        
        if ([attribute hasPrefix:@"Tc"]) { // BOOL: Tc
            BOOL value = NO;
            if ([propertyValue respondsToSelector:@selector(boolValue)])
                value = [propertyValue boolValue];
            [object setValue:[NSNumber numberWithBool:value] forKey:propertyName];
        } else if ([attribute hasPrefix:@"Ti"]) { // int: Ti
            int value = 0;
            if ([propertyValue respondsToSelector:@selector(intValue)])
                value = [propertyValue intValue];
            [object setValue:[NSNumber numberWithInt:value] forKey:propertyName];
        } else if ([attribute hasPrefix:@"Tq"]) { // long long: Tq
            long long value = 0;
            if ([propertyValue respondsToSelector:@selector(longLongValue)])
                value = [propertyValue longLongValue];
            [object setValue:[NSNumber numberWithLongLong:value] forKey:propertyName];
        } else if ([attribute hasPrefix:@"Td"]) { // double: Td
            double value = 0.0;
            if ([propertyValue respondsToSelector:@selector(doubleValue)])
                value = [propertyValue doubleValue];
            [object setValue:[NSNumber numberWithDouble:value] forKey:propertyName];
        } else if ([attribute hasPrefix:@"T@\"NSString\""]) { // NSString: T@"NSString"
            NSString *value = nil;
            if ([propertyValue isKindOfClass:[NSString class]])
                value = propertyValue;
            [object setValue:value forKey:propertyName];
        } else if ([attribute hasPrefix:@"T@\"NSDate\""]) { // NSDate: T@"NSDate"
            NSDate *value = nil;
            if ([propertyValue doubleValue]>0)
                value = [NSDate dateWithUNIXTimeString:propertyValue];
            [object setValue:value forKey:propertyName];
        } else {
            [object setValue:propertyValue forKey:propertyName];
        }
    }
    
    object.list = [NSArray objectWithXMLNode:XMLNode];
    
    return [object autorelease];
}

- (NSString *)description {
    return [_list description];
}

@end
